package localizer.main;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.location.Location;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.SmsManager;
import android.util.Log;

public class UpdateTaskService extends IntentService{

	static String username;
	static String id;
	static Location currentBestLocation;
	Config config;
	private static LocationManager locationManager;
	private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 0; // in Meters
	private static final long MINIMUM_TIME_BETWEEN_UPDATES = 0; // in Milliseconds
	private static final long TIME_BETWEEN_UPDATES = 1000 * 60 * 5;
	private static final long TIME_WAITING_UPDATES = 1000;
	private static final int SIGNIFICANT_TIME_TO_CHANGE = 1000;
	private static final String TAG = "QuickNotesMainActivity";
	
	public UpdateTaskService() {
		super("update service");
		
	}

	@Override
	protected void onHandleIntent(Intent arg0) {
		ConnectivityManager cm = (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE); 
		NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
		boolean isConnected  =  ((activeNetwork != null) && activeNetwork.isConnected());
		if( ! isConnected){
			Log.d("UpdadeTask", "sem net, fexoo");
			return;
		}
		config = Config.getInstance(getApplicationContext());
		username = config.getUsername(getApplicationContext());
		id = config.getID(getApplicationContext());
		config = null;
		Log.d(username, id + "  esta " + isConnected);
		ExemploCameraActivity.username=username;
		ExemploCameraActivity.id=id;
		
		//funcao para pegar tasks
		
		String url = LoginActivity.SITE + "/activity/get/?username="+username+"&identifier="+id;
	    HttpClient httpclient = new DefaultHttpClient();
	    HttpGet httget = new HttpGet(url);
	    String responseST = "";
	    try {
	        
	        HttpResponse response = httpclient.execute(httget);
	        HttpEntity responseEntity = response.getEntity();
	        String[] tasks = {};
	        if(responseEntity!=null) {
	            responseST = EntityUtils.toString(responseEntity);
	            tasks = responseST.split("-");
	        }
	        Log.d("task", responseST);
	        for( int i = 0; i < tasks.length; i++){
		        if(tasks[i].equals("1")){
		        	Intent dialogIntent = new Intent(getBaseContext(), RingActivity.class);
		        	dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		        	dialogIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
		        	dialogIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
		        	dialogIntent.putExtra("tipe", "ring");
		        	if( Config.getInstance(getApplicationContext()).isEnableCheckRing(getApplicationContext())) getApplication().startActivity(dialogIntent);
					
					try{
						String url2 = LoginActivity.SITE + "/activity/receive/?username="+username+"&identifier="+id+"&activity=1";
					    HttpClient httpclient2 = new DefaultHttpClient();
					    HttpGet httget2 = new HttpGet(url2);
					    HttpResponse response2 = httpclient2.execute(httget2);
					} catch ( Exception e ){
						
					}
		        }
		        if(tasks[i].equals("0")){
		        	Intent dialogIntent = new Intent(getBaseContext(), ExemploCameraActivity.class);
		        	dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		        	dialogIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
		        	dialogIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
		        	getApplication().startActivity(dialogIntent);
		        	try{
						String url2 = LoginActivity.SITE + "/activity/receive/?username="+username+"&identifier="+id+"&activity=0";
					    HttpClient httpclient2 = new DefaultHttpClient();
					    HttpGet httget2 = new HttpGet(url2);
					    HttpResponse response2 = httpclient2.execute(httget2);
					} catch ( Exception e ){
					}
		        	
		        }
		        if(tasks[i].equals("2")){
		        	Intent dialogIntent = new Intent(getBaseContext(),RingActivity.class );
		        	dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		        	dialogIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
		        	dialogIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
		        	dialogIntent.putExtra("tipe", "silent");
		        	getApplication().startActivity(dialogIntent);
		        	try{
						String url2 = LoginActivity.SITE + "/activity/receive/?username="+username+"&identifier="+id+"&activity=2";
					    HttpClient httpclient2 = new DefaultHttpClient();
					    HttpGet httget2 = new HttpGet(url2);
					    HttpResponse response2 = httpclient2.execute(httget2);
					} catch ( Exception e ){
					}
		        	
		        }
	        }
	        
	    } catch (Exception e) {
	        Log.d("ERRO na atividade", e.getMessage());
	    }	
	    Log.d("update Task", "fim service");
	}
	
	
	
}
